//
//  PlayerManage.m
//  movieInfo
//
//  Created by zhangkai on 2023/5/28.
//

#import "PlayerManage.h"
#import "ZipArchive.h"
#import "TMMovieModel.h"
@implementation PlayerManage


+ (instancetype)defaultManager;{
    
    static PlayerManage *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[PlayerManage alloc] init];
        manager.currentMovie = [[TMMovieInfoModel alloc] init];
    });
    return manager;

}



- (NSString *)playUrl;{

    NSArray *subitArr = [PlayerManage defaultManager].currentMovie.subtitleArr;
    if(subitArr.count > 0){
        
        TMMovieModel *firstDic = subitArr.firstObject;
        for (TMMovieModel *tempDic in subitArr) {
            
//            if([[tempDic objectForKey:@"lang"] isEqualToString:@"en"]){
//                firstDic = tempDic;
//            }
            
            
            if([tempDic.lang isEqualToString:@"en"]){
                firstDic = tempDic;
            }
            
            
            
        }
        
        NSString *zipStr = firstDic.sub;
        NSURL *zipUrl = [NSURL URLWithString:zipStr];
        zipStr = [zipUrl lastPathComponent];
        
        
        NSString *strName = [[[zipStr lastPathComponent] stringByDeletingPathExtension] stringByDeletingLastPathComponent];
        
        NSData *curData = [NSData dataWithContentsOfURL:zipUrl];
        NSString *file = [NSString stringWithFormat:@"%@/%@",[self getLocSubTlePath],zipStr];
        [curData writeToURL:[NSURL fileURLWithPath:file] atomically:YES];
        NSString *unfile = [NSString stringWithFormat:@"%@/%@",[self getLocSubTlePath],strName];

        ZipArchive *zip = [[ZipArchive alloc] initWithFileManager:[NSFileManager defaultManager]];
        if([zip UnzipOpenFile:file]){
            BOOL ttret = [zip UnzipFileTo:[self getLocSubTlePath] overWrite:YES];
            if(ttret){
                
                [zip UnzipCloseFile];
                NSError *error = nil;
                NSString *newStr = [NSString stringWithContentsOfFile:unfile encoding:NSUTF8StringEncoding error:&error];
                
                NSError *error1 = nil;
                NSError *error2 = nil;
                [[NSFileManager defaultManager] removeItemAtPath:file error:&error1];
                [[NSFileManager defaultManager] removeItemAtPath:unfile error:&error2];
//                播放
                return newStr;
                
                
            }
 
        }
    }
    
    return @"";
}


- (NSString *)getLocSubTlePath{
//    NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString *userPath = [docPath stringByAppendingPathComponent:@"TMDownload"];
    NSFileManager *manage = [NSFileManager defaultManager];
    if(![manage fileExistsAtPath:userPath]){
        NSError *error = nil;
        [manage createDirectoryAtPath:userPath withIntermediateDirectories:YES attributes:nil error:&error];
    }
    
    return userPath;
    
}


//static func SubttlePath()->String{
//    let path = (RootPath() as NSString).appendingPathComponent("Subttle")
//    ensurePathAt(path: path)
//    return path
//}
@end
